package com.kdtech.analyse.news;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
/**
 * 
 * @author axi
 *
 */
public class SmgbbNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://www.kankanews.com/[a-z]*/[0-9-0-9-0-9]*/[0-9]*.shtml"
		};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url=urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
		}
		String htmltxt=urlMeta.getHtml();
		String title="";
		String content="";
		Long date=null;
		Integer commentNum=0;
		Integer clickNum=0;
		NewsMeta news=new NewsMeta();
		Document doc=Jsoup.parse(htmltxt);
		/**
		 * 解析 标题 <h1>XXXXXXXXXX</h1> 定位
		 */
		title=doc.select("h1").text();
		if("".equals(title)){
		}
		Elements select=doc.select("div.wbox");
		if(select.size() > 0){
			/**
			 * 解析内容
			 */
			Elements selectContent=select.select("div#pcontent");
			if(selectContent.size() > 0){
				content=HtmlCleaner.getContentHtml(url,selectContent);
			}else{
				selectContent=select.select("div.intor");
				if(selectContent.size() > 0){
					content=HtmlCleaner.getContentHtml(url,selectContent);
				}
			}
		}
		date=DateUtils.matchDate(doc.select("#pubtime_baidu").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("#upload,.fbtime").text());
			if(date==null){
				date=DateUtils.matchDate(url);
			}
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.textBody"));
		}
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		news.setUrl(url);
		news.setType(0);
		news.setCommentNum(commentNum);
		news.setClickNum(clickNum);
		
		String author=doc.select("div.content div.show-pic span em").text();
		if(author!=null && author.indexOf("作者：")!=-1){
			author=StringUtils.substringAfterLast(author, "作者：");
		}
		if(StringUtils.isBlank(author)){
			author=doc.select("div.content div.handle span").text();
			if(author.indexOf("来源：")!=-1){
				author=StringUtils.substringAfterLast(author, "来源：").trim();
			}
		}
		news.setAuthor(author);
		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SmgbbNewsAnalyse s=new SmgbbNewsAnalyse();
		String url="http://www.kankanews.com/a/2016-10-11/0037720242.shtml";
		System.out.println(s.isDetailPage(url));
//		String url="http://society.kankanews.com/baitai/2012-11-22/1861104.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=s.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
